package com.nvt.core.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.nvt.core.dao.de.GenericResultSetExtractor;
import com.nvt.core.exception.NvtBusinessException;

@Service
public class JdbcDAOImpl implements IJdbcDAO {

	private String insertString = "INSERT INTO <TABLE_NAME> VALUES (?,?,?,?,?)";

	private String updateString = "UPDATE <TABLE_NAME> SET <PARAM_NAME> = <PARAM_VALUE>";

	private String findAllString = "SELECT * FROM <TABLE_NAME>";

	private String findByParamString = "SELECT * FROM <TABLE_NAME> WHERE <PARAM_NAME> = <PARAM_VALUE>";

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void executeDelete(String entityName, Object[] values)
			throws NvtBusinessException {
		try {
			updateString = updateString.replace("<TABLE_NAME>", entityName);
			updateString = updateString.replace("<PARAM_NAME>", values[0]
					.toString().split(":")[0]);
			updateString = updateString.replace("<PARAM_VALUE>", values[0]
					.toString().split(":")[1]);

			jdbcTemplate.update(updateString);
		} catch (Exception e) {
			throw new NvtBusinessException();
		}
	}

	@Override
	public Integer executeInsert(String entityName, Object[] values)
			throws NvtBusinessException {
		try {
			insertString = insertString.replace("<TABLE_NAME>", entityName);
			jdbcTemplate.update(insertString, values);
			return null;
		} catch (Exception e) {
			throw new NvtBusinessException();
		}
	}

	@Override
	public void executeUpdate(String entityName, Object[] values)
			throws NvtBusinessException {
		try {
			updateString = updateString.replace("<TABLE_NAME>", entityName);
			updateString = updateString.replace("<PARAM_NAME>", values[0]
					.toString().split(":")[0]);
			updateString = updateString.replace("<PARAM_VALUE>", values[0]
					.toString().split(":")[1]);

			jdbcTemplate.update(updateString);
		} catch (Exception e) {
			throw new NvtBusinessException();
		}
	}

	@Override
	public List<Map<String, Object>> findAll(String entityName)
			throws NvtBusinessException {
		try {
			findAllString = findAllString.replace("<TABLE_NAME>", entityName);
			List<Map<String, Object>> list = jdbcTemplate.query(findAllString,
					new GenericResultSetExtractor());
			return list;
		} catch (Exception e) {
			throw new NvtBusinessException();
		}
	}

	@Override
	public List<Map<String, Object>> findByParameters(String entityName,
			Object[] values) throws NvtBusinessException {
		try {
			findByParamString = findByParamString.replace("<TABLE_NAME>",
					entityName);
			findByParamString = findByParamString.replace("<PARAM_NAME>",
					values[0].toString().split(":")[0]);
			findByParamString = findByParamString.replace("<PARAM_VALUE>",
					values[0].toString().split(":")[1]);
			List<Map<String, Object>> list = jdbcTemplate.query(
					findByParamString, new GenericResultSetExtractor());
			return list;
		} catch (Exception e) {
			throw new NvtBusinessException();
		}
	}

}
